﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Winsmarts.WCFSupport
{
    public class WCFSupportReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp != null)
            {
                RemoveModifications(webApp);
                AddModifications(webApp);
                SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            }

            //SPService service = (SPService)properties.Feature.Parent;
            //SPServiceCollection services = service.Farm.Services;
            //SPWebApplicationCollection WebApps = services.GetValue<SPWebService>().WebApplications;
            //foreach (SPWebApplication WebApp in WebApps)
            //{
            //    foreach (ModificationEntry entry in Entries)
            //    {
            //        WebApp.WebConfigModifications.Add(CreateModification(entry.Name, entry.XPath, entry.Value)
            //        );
            //    }
            //    SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            //}
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp != null)
            {
                RemoveModifications(webApp);
                SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            }

            //SPService service = (SPService)properties.Feature.Parent;
            //SPServiceCollection services = service.Farm.Services;
            //SPWebApplicationCollection WebApps = services.GetValue<SPWebService>().WebApplications;
            //foreach (SPWebApplication WebApp in WebApps)
            //{
            //    foreach (ModificationEntry entry in Entries)
            //    {
            //        WebApp.WebConfigModifications.Remove(CreateModification(entry.Name, entry.XPath, entry.Value)
            //        );
            //    }
            //    SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            //}
        }

        private void AddModifications(SPWebApplication webApp)
        {
            foreach (ModificationEntry entry in Entries)
            {
                webApp.WebConfigModifications.Add(CreateModification(entry.Name, entry.XPath, entry.Value));
            }
        }
        
        private void RemoveModifications(SPWebApplication webApp)
        {
            foreach (ModificationEntry entry in Entries)
            {
                SPWebConfigModification mod = CreateModification(entry.Name, entry.XPath, entry.Value);
                while (webApp.WebConfigModifications.Remove(mod))
                { /* Remove all matching modifications */ }
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            // No Implementation
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            // No Implementation
        }

#region "Web.Config Modification"
        private struct ModificationEntry
        {
            public string Name;
            public string XPath;
            public string Value;
            public ModificationEntry(string Name, string XPath, string Value)
            {
                this.Name = Name;
                this.XPath = XPath;
                this.Value = Value;
            }
        }

        private ModificationEntry[] Entries = {
              new ModificationEntry(
                  "add[@name='WCFVPPRegModule']",
                   "configuration/system.web/httpModules",
                   @"<add name=""WCFVPPRegModule"" type=""Winsmarts.WCFSupport.WCFVPPRegModule, Winsmarts.WCFSupport, Version=1.0.0.0, Culture=neutral,PublicKeyToken=2724eeeca67c35a7"" />"
                   )
            };

        public SPWebConfigModification CreateModification(string Name, string XPath, string Value)
        {
            SPWebConfigModification modification = new SPWebConfigModification(Name, XPath);
            modification.Owner = "Winsmarts.WCFSupport";
            modification.Sequence = 0;
            modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            modification.Value = Value;
            return modification;
        }
#endregion
    }
}
